# coding: utf-8
"""
爬取策略：
1.phantomJS操作异常
"""
import configparser
import datetime
import os
import json
import time
import re
from urllib import parse

import requests
import sys
from lxml import etree
from redis import Redis
from selenium import webdriver


class XiechengAirport(object):
    def __init__(self, url='http://flights.ctrip.com/hot-airport.html',
                 query_url='http://m.ctrip.com/restapi/soa2/12812/getpoicontent',
                 query_ticket_url='http://www.ctrip.com/'):
        self.url = url
        self.query_url = query_url
        self.query_ticket_url = query_ticket_url
        self.rd = Redis()
        self.driver = webdriver.Chrome()
        self.rediskey = 'xiecheng_airport'

        _path = os.path.join(os.path.abspath('.'), '../')
        sys.path.append(_path)
        self.ei = __import__('Utils.C002_extract_info', fromlist=['C002_extract_info'])
        self.ua_file = {'pc': '{0}Utils/{1}'.format(_path, self.ei.ExtractInfo.ua_file['pc'])}
        self.user_agent = self.ei.ExtractInfo.get_useragent(file=self.ua_file)

    def parse_code(self, area):
        # TODO 解析地区编码接口改变
        _query_url = '{0}?channel=1&mode=1&f=2&key={1}'.format(self.query_url, parse.quote(area))
        self.user_agent = self.ei.ExtractInfo.get_useragent(file=self.ua_file)
        print(_query_url)
        _respose = requests.get(_query_url, headers=self.user_agent)
        print(_respose.status_code, _respose.text)
        _code = _respose.json()['content']
        _code = json.loads(_code)['Data'][0]['Code']
        return _code

    def parse_airport(self, areas):
        _airports = []
        for area in areas:
            _temp = {}
            _temp['name'] = area
            _temp['code'] = self.parse_code(area)
            _airports.append(_temp)
        return _airports

    def parse_date(self, date):
        _dates = []
        for i in range(366):
            _temp = (date + datetime.timedelta(days=i)).strftime("%Y-%m-%d")
            _dates.append(_temp)
        return _dates

    def parse_param(self):
        self.driver.get(self.query_ticket_url)
        time.sleep(3)
        _flight = self.driver.find_element_by_xpath('//*[@id="searchBoxUl"]/li[2]')
        _flight.click()
        time.sleep(1)

        _departure = self.driver.find_element_by_xpath('//*[@id="FD_StartCity"]')
        _start_date = self.driver.find_element_by_xpath('//*[@id="FD_StartDate"]')
        _destination = self.driver.find_element_by_xpath('//*[@id="FD_DestCity"]')
        # 防止元素点不到
        search = self.driver.find_element_by_xpath('//*[@id="searchBox"]/div[2]')
        _departure.clear()
        _departure.send_keys("上海")
        _start_date.send_keys("2017-11-28")
        _destination.send_keys("北京")
        search.click()
        time.sleep(1)
        _submit = self.driver.find_element_by_xpath('//*[@id="FD_StartSearch"]')
        _submit.click()
        time.sleep(5)

        _el_user = self.driver.find_element_by_xpath('//*[@id="sso_txtUid"]')
        _el_pswd = self.driver.find_element_by_xpath('//*[@id="sso_txtPwd"]')
        _submit_auth = self.driver.find_element_by_xpath('//*[@id="sso_btnSubmit"]')
        _user_pswd_cfg = os.environ['user_pswd']
        _cf = configparser.ConfigParser()
        _cf.read(_user_pswd_cfg)
        _user = _cf.get('xiecheng', 'xiecheng_user')
        _pswd = _cf.get('xiecheng', 'xiecheng_pswd')
        _user.click()
        _el_user.send_keys(_user)
        _el_pswd.send_keys(_pswd)
        _content = re.findall(r'LogToken=([^&]*?)&amp;CK=([^"]*?)"', self.driver.page_source)[0]
        _submit_auth.click()

        _params = {}
        _params['log_token'], _params['ck'] = _content
        _cookies = {}
        _cookies_list = self.driver.get_cookies()  # 这里返回的是一个更多信息的字典列表
        for _infos in _cookies_list:
            _cookies[_infos['name']] = _infos['value']
        return _cookies, _params

    def run(self):
        _response = requests.get(self.url, headers=self.user_agent)
        _html = etree.HTML(_response.content.decode(encoding='gbk'))
        # 获取当前日期
        _now = datetime.datetime.now()
        # 获取全国的机场
        _areas = _html.xpath('//*[@class="schedule_list clearfix"]/li/a/text()')
        # # 获取全国机场的代码
        _airports = self.parse_airport(_areas)
        print(_airports)
        # # 获取全部订票日期
        # _dates = self.parse_date(_now)
        # # 获取提取参数
        # _cookies, _params = self.parse_param()
        #
        # _col_data = {}
        # _col_data['col_time'] = str(_now)
        # _col_data['airports'] = _airports
        # _col_data['date'] = _dates
        # _col_data['param'] = _params
        # _col_data['cookie'] = _cookies
        # print(_col_data)
        # self.rd.rpush(self.rediskey, _col_data)


def main():
    xc = XiechengAirport()
    xc.run()


if __name__ == '__main__':
    main()
